package com.loveshare.log.dao;

import java.sql.Timestamp;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.loveshare.log.bean.UservisitlogInfo;

/**
 * 
 * ClassName: LogDaoImpl 
 * @Description: 访问日志数据接口实现类
 * @author 爱分享
 * @date 2017年12月20日
 */
@Repository
public class LogDaoImpl implements LogDao{

	@Autowired
	JdbcTemplate jdbcTemplate;

	public boolean addArticleInfo(String ip,String ipregion,int indexType) {
		String sql="INSERT INTO Uservisitlog (ip,ipregion,visitTime,indexType) VALUES (?,?,?,?)";
		jdbcTemplate.update(sql,ip,ipregion,new Timestamp(System.currentTimeMillis()),indexType);
		return true;
	}

	public long getArticleInfoCount() {
		String sql="SELECT COUNT(1) FROM Uservisitlog";
		return jdbcTemplate.queryForObject(sql, Long.class);
	}

	public UservisitlogInfo getUservisitlogInfo(String ip) {
		try {
			RowMapper<UservisitlogInfo> mapper = new BeanPropertyRowMapper<UservisitlogInfo>(UservisitlogInfo.class);
			UservisitlogInfo log = jdbcTemplate.queryForObject("SELECT ip,ipregion FROM `Uservisitlog` WHERE ip=? LIMIT 1", mapper, ip);
			return log;
		} catch (Exception e) {
			return null;
		}
		
	}

}
